export OS_NAME := $(shell uname -o)
$(info curren system $(OS_NAME))

udpserver_main_test = udpserver_main_test.o
udpsend_main_test = udpsend_main_test.o
savelog_test = savelog_test.o
tcpserver_cmd_test = tcpserver_cmd_test.o
tcpclient_cmd_test = tcpclient_cmd_test.o

kcpclient_cmd_test = kcpclient_cmd_test.o
kcpserver_cmd_test = kcpserver_cmd_test.o

broadme = broadme.o
upgrade_file_test = upgrade_file_test.o
upgrade_sendfile_test = upgrade_sendfile_test.o

kcpsend_main_test = kcpsend_main_test.o

LDFLAGS += -Llibpbc/build/ -lpbc -Llibdi/ -ldi -Llibudpserver -ludpserver -Llibudpsend -ludpsend -Llibupgrade -lupgrade -Llibkcpsend -lkcpsend -Llibkcpserver -lkcpserver -Llibkcpclient -lkcpclient -Llibikcp -likcp
CFLAGS += -O0 -fPIC -Wall -g -Ilibdi/ -Ilibpbc/ -Ilibudpserver -Ilibudpsend -Ilibdu -Ilibupgrade -Ilibkcpsend -Ilibkcpclient -Ilibkcpserver -Llibikcp

CFLAGS += -Ilibfile -Iliblog
LDFLAGS += -Llibfile -lfile -Lliblog -llog

CFLAGS += -Ilibcmd -Ilibtcpserver
LDFLAGS += -Llibcmd -lcmd -Llibtcpserver -ltcpserver

CFLAGS += -Ilibcmd -Ilibkcpserver
LDFLAGS += -Llibcmd -lcmd -Llibkcpserver -lkcpserver

CFLAGS += -Ilibtcpclient
LDFLAGS += -Llibtcpclient -ltcpclient

CFLAGS += -Ilibkcpclient
LDFLAGS += -Llibkcpclient -lkcpclient

export CC ?= gcc
export INSTALL ?= @echo "install $@" && $(CC)

MODULES := 

.PHONY : all allmodules libfile liblog di pbc kcpserver kcpsend ikcp udpserver udpsend tcpserver cmd $(TEST_PROG)

all: makedir proto media_device allmodules alltest
###########################################################################################################################################################
# 下面是编译库
# #########################################################################################################################################################

####################
# libipcclient
# ##################
LDFLAGS += -Llibipcclient -lipcclient
CFLAGS  += -Ilibipcclient
MODULES += ipcclient
ipcclient:
	make -C libipcclient

###################
# libipcserver
# #################
LDFLAGS += -Llibipcserver -lipcserver
CFLAGS  += -Ilibipcserver
MODULES += ipcserver
ipcserver:
	make -C libipcserver


###################
# libdu
# #################
LDFLAGS += -Llibdu -ldu
CFLAGS  += -Ilibdu
MODULES += du
du:
	make -C libdu

###########################################################################################################################################################
# 下面是编译测试程序
# #########################################################################################################################################################
define testapp
#$(1) := $(1).o
#$(1) := $(addprefix build/,$($(1)))
#TEST_PROG += $(1)
#$(1):$(1)
#	$(INSTALL) -o $@ $(1) $(LDFLAGS) -lpthread
endef

udpserver_main_test := $(addprefix build/,$(udpserver_main_test))
udpsend_main_test := $(addprefix build/,$(udpsend_main_test))
savelog_test := $(addprefix build/,$(savelog_test))
tcpserver_cmd_test := $(addprefix build/,$(tcpserver_cmd_test))
tcpclient_cmd_test := $(addprefix build/,$(tcpclient_cmd_test))

kcpserver_cmd_test := $(addprefix build/,$(kcpserver_cmd_test))
kcpclient_cmd_test := $(addprefix build/,$(kcpclient_cmd_test))

broadme := $(addprefix build/,$(broadme))
upgrade_file_test := $(addprefix build/,$(upgrade_file_test))
upgrade_sendfile_test := $(addprefix build/,$(upgrade_sendfile_test))


kcpsend_main_test := $(addprefix build/,$(kcpsend_main_test))

TEST_PROG  := udpserver_main_test kcpsend_main_test udpsend_main_test savelog_test tcpserver_cmd_test tcpclient_cmd_test kcpserver_cmd_test kcpclient_cmd_test broadme upgrade upgrade_file_test upgrade_sendfile_test

##################
# ipcclient_test
# ################
ipcclient_cmd_test := ipcclient_cmd_test.o
ipcclient_cmd_test := $(addprefix build/,$(ipcclient_cmd_test))
TEST_PROG += ipcclient_cmd_test
ipcclient_cmd_test: $(ipcclient_cmd_test)
	 $(INSTALL) -o $@  $($@) $(LDFLAGS) -lpthread

###################
# ipcserver_test
# #################
ipcserver_cmd_test := ipcserver_cmd_test.o
ipcserver_cmd_test := $(addprefix build/,$(ipcserver_cmd_test))
TEST_PROG += ipcserver_cmd_test
ipcserver_cmd_test: $(ipcserver_cmd_test)
	 $(INSTALL) -o $@  $($@) $(LDFLAGS) -lpthread



###########################################################################################################################################################
# 下面是旧的编译规则
# #########################################################################################################################################################
makedir:
	mkdir -p build/
	protoc car.proto -ocar.pb

upgrade_sendfile_test:$(upgrade_sendfile_test) di tcpclient libfile
	$(INSTALL) -o $@  $(upgrade_sendfile_test) $(LDFLAGS) -lpthread

upgrade_sendfile_test:$(upgrade_sendfile_test) di kcpclient libfile
	$(INSTALL) -o $@  $(upgrade_sendfile_test) $(LDFLAGS) -lpthread
	
upgrade_file_test:$(upgrade_file_test) di tcpserver libfile upgrade
	 $(INSTALL) -o $@  $(upgrade_file_test) $(LDFLAGS) -lpthread

upgrade_file_test:$(upgrade_file_test) di kcpserver libfile upgrade
	 $(INSTALL) -o $@  $(upgrade_file_test) $(LDFLAGS) -lpthread

broadme: $(broadme) di tcpclient cmd
	$(INSTALL) -o $@  $(broadme)  $(LDFLAGS) -lpthread

broadme: $(broadme) di kcpclient cmd
	$(INSTALL) -o $@  $(broadme)  $(LDFLAGS) -lpthread

tcpclient_cmd_test: $(tcpclient_cmd_test) di tcpclient cmd
	$(INSTALL) -o $@ $(tcpclient_cmd_test) $(LDFLAGS) -lpthread

kcpclient_cmd_test: $(kcpclient_cmd_test) di kcpclient cmd
	$(INSTALL) -o $@ $(kcpclient_cmd_test) $(LDFLAGS) -lpthread
	
tcpserver_cmd_test: $(tcpserver_cmd_test) di tcpserver cmd
	$(INSTALL) -o $@ $(tcpserver_cmd_test) $(LDFLAGS) -lpthread

kcpserver_cmd_test : $(kcpserver_cmd_test) di cmd kcpserver ikcp
	$(INSTALL) -o $@ $(kcpserver_cmd_test) $(LDFLAGS) -lpthread

udpserver_main_test : $(udpserver_main_test) di pbc udpserver udpsend 
	$(INSTALL) -o $@ $(udpserver_main_test) $(LDFLAGS)
		
udpsend_main_test: $(udpsend_main_test) di pbc udpserver udpsend
	$(INSTALL) -o $@ $(udpsend_main_test) $(LDFLAGS)

kcpsend_main_test: $(kcpsend_main_test) di pbc kcpserver kcpsend kcpclient ikcp
	$(INSTALL) -o $@ $(kcpsend_main_test) $(LDFLAGS)

savelog_test: di libfile liblog $(savelog_test)
	$(INSTALL) -o $@ $(savelog_test) $(LDFLAGS)

build/%.o:%.c
	@echo "completing $@...." && $(CC) -c -o $@ $^ $(CFLAGS)

tcpclient:
	make -C libtcpclient

kcpclient:
	make -C libkcpclient
	
tcpclient_android:
	make -C libtcpclient_android
	
tcpserver:
	make -C libtcpserver

kcpserver:
	make -C libkcpserver

cmd:
	make -C libcmd
	
di:
	make -C libdi

pbc:
	make -C libpbc

udpserver:
	make -C libudpserver
	
udpsend:
	make -C libudpsend

kcpclient:
	make -C libkcpclient

kcpsend:
	make -C libkcpsend
ikcp:
	make -C libikcp

libfile:
	make -C libfile

liblog:
	make -C liblog

upgrade:
	make -C libupgrade
	
appwidget_modules:
	make -C appwidget
	
clean:
	rm -rf build/
	rm -rf tools/bin2hex
	rm -rf $(TEST_PROG)
	make -C libdi clean
	make -C libpbc clean
	make -C libkcpserver clean
	make -C libudpserver clean
	make -C libudpsend clean
	make -C libkcpsend clean	
	make -C libkcpclient clean
	make -C libikcp clean
	make -C libfile clean
	make -C liblog clean
	make -C libtcpserver clean
	make -C libkcpserver clean
	make -C libcmd clean
	make -C libtcpclient clean
	make -C libkcpclient clean
	make -C libupgrade clean
	make -C libipcclient clean
	make -C libipcserver clean
	make -C libdu clean
	make -C appwidget clean
	
proto:
	make -C tools
	
media_device: 
	protoc media_device.proto -omedia_device.pb
	./tools/bin2hex media_device < media_device.pb 1>media_device.h	

allmodules: cmd tcpclient tcpserver libfile liblog di pbc kcpserver kcpsend kcpclient ikcp udpserver udpsend upgrade $(MODULES) appwidget_modules
alltest:  $(TEST_PROG)

